package com.gmail.blackdog1987.onedollar.dao;

import com.gmail.blackdog1987.onedollar.dao.hibernate.EntityHibernateDao;
import com.gmail.blackdog1987.onedollar.dao.hibernate.jdbc.BaseJdbcDao;
import com.gmail.blackdog1987.onedollar.dao.hibernate.jdbc.JdbcUtil;
import com.gmail.blackdog1987.onedollar.entity.TGoodsShowEntity;
import com.gmail.blackdog1987.onedollar.entity.TOrderInfoEntity;
import com.gmail.blackdog1987.onedollar.entity.TOrderOnedollarEntity;
import com.gmail.blackdog1987.onedollar.entity.base.Page;
import com.gmail.blackdog1987.onedollar.vo.CartVO;
import com.gmail.blackdog1987.onedollar.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository("orderDao")
public class OrderDao extends EntityHibernateDao<TOrderInfoEntity>{
    @Autowired
    private BaseJdbcDao baseJdbcDao;

    public Page<OrderVO> queryByUserid(String userid, int pageindex, int pagesize){
        String sql =
                "SELECT t_order_goods.*, t_order_info.*, t_goods_picture.pic_url\n";
        String where =
                "                FROM t_order_info,\n" +
                "                     t_order_goods\n"+
                "                LEFT JOIN t_goods_picture ON t_order_goods.goods_id = t_goods_picture.goods_id AND t_goods_picture.is_main = 1\n"+
                "                WHERE t_order_goods.order_id = t_order_info.order_id\n" +
                "                AND t_order_info.invalid = 0\n"+
                "                AND t_order_info.user_id = ?\n";
        String orderBy =
                "                ORDER BY t_order_info.pay_time DESC, t_order_info.add_time DESC ";
        Object [] args = new Object[]{userid};

        String limit = " limit " + (pageindex-1)*pagesize + "," + pagesize;
        Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" SELECT COUNT(*) " + where , args);

        List<OrderVO> list = JdbcUtil.queryForList(baseJdbcDao, OrderVO.class, sql + where + orderBy + limit, args);

        Page page = new Page(list, pageindex, totalCount, pagesize);

        return page;
    }

    public OrderVO queryById(int userid, int orderid){
        String sql =
                "SELECT t_order_goods.*, t_order_info.*, t_goods_picture.pic_url\n";
        String where =
                "                FROM t_order_info,\n" +
                        "                     t_order_goods\n"+
                        "                LEFT JOIN t_goods_picture ON t_order_goods.goods_id = t_goods_picture.goods_id AND t_goods_picture.is_main = 1\n"+
                        "                WHERE t_order_goods.order_id = t_order_info.order_id\n" +
                        "                AND t_order_info.order_id = ?\n"+
                        "                AND t_order_info.user_id = ?\n";

        List<OrderVO> list = JdbcUtil.queryForList(baseJdbcDao, OrderVO.class, sql + where, new Object[]{orderid, userid});
        return list==null||list.isEmpty()?null:list.get(0);
    }

    public List<TOrderInfoEntity> queryByIds(String orderid){
        return JdbcUtil.queryForList(baseJdbcDao, TOrderInfoEntity.class, "SELECT * FROM t_order_info WHERE t_order_info.order_id in ("+orderid+")", new Object[]{});
    }

    public Page<OrderVO> queryNotCommentByUserid(String userid, int pageindex, int pagesize){
        String sql =
                "SELECT t_order_goods.*, t_order_info.*, t_goods_picture.pic_url\n";
        String where =
                "                FROM t_order_info,\n" +
                "                     t_order_goods\n"+
                "                LEFT JOIN t_goods_picture ON t_order_goods.goods_id = t_goods_picture.goods_id AND t_goods_picture.is_main = 1\n"+
                "                WHERE t_order_goods.order_id = t_order_info.order_id\n" +
                "                AND t_order_info.user_id = ?\n"+
                "                AND t_order_info.order_status = 5\n"+
                "                AND t_order_info.invalid =0\n"+
                "                AND t_order_info.is_refund = 0\n"+
                "                AND t_order_info.judge_status = 0\n";
        String orderBy =
                "                ORDER BY t_order_info.pay_time DESC, t_order_info.add_time DESC ";
        Object [] args = new Object[]{userid};

        String limit = " limit " + (pageindex-1)*pagesize + "," + pagesize;
        Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" SELECT COUNT(*) " + where , args);

        List<OrderVO> list = JdbcUtil.queryForList(baseJdbcDao, OrderVO.class, sql + where + orderBy + limit, args);

        Page page = new Page(list, pageindex, totalCount, pagesize);

        return page;
    }

    public Page<OrderVO> queryNotPayByUserid(String userid, int pageindex, int pagesize){
        String sql =
                "SELECT t_order_goods.*, t_order_info.*, t_goods_picture.pic_url\n";
        String where =
                "                FROM t_order_info,\n" +
                "                     t_order_goods\n"+
                "                LEFT JOIN t_goods_picture ON t_order_goods.goods_id = t_goods_picture.goods_id AND t_goods_picture.is_main = 1\n"+
                "                WHERE t_order_goods.order_id = t_order_info.order_id\n" +
                "                AND t_order_info.user_id = ?\n"+
                "                AND t_order_info.pay_status = 0\n"+
                "                AND t_order_info.invalid =0\n"+
                "                AND t_order_info.order_status = 1\n";
        String orderBy =
                "                ORDER BY t_order_info.pay_time DESC, t_order_info.add_time DESC ";
        Object [] args = new Object[]{userid};

        String limit = " limit " + (pageindex-1)*pagesize + "," + pagesize;
        Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" SELECT COUNT(*) " + where , args);

        List<OrderVO> list = JdbcUtil.queryForList(baseJdbcDao, OrderVO.class, sql + where + orderBy + limit, args);

        Page page = new Page(list, pageindex, totalCount, pagesize);

        return page;
    }
    public Page<OrderVO> queryNotFinishByUserid(String userid, int pageindex, int pagesize){
        String sql =
                "SELECT t_order_goods.*, t_order_info.*, t_goods_picture.pic_url\n";
        String where =
                "                FROM t_order_info,\n" +
                "                     t_order_goods\n"+
                "                LEFT JOIN t_goods_picture ON t_order_goods.goods_id = t_goods_picture.goods_id AND t_goods_picture.is_main = 1\n"+
                "                WHERE t_order_goods.order_id = t_order_info.order_id\n" +
                "                AND t_order_info.order_status = 1\n"+
                "                AND t_order_info.invalid =0\n"+
                "                AND t_order_info.pay_status = 1\n"+
                "                AND t_order_info.shipping_status = 1\n"+
                "                AND t_order_info.user_id = ?\n";
        String orderBy =
                "                ORDER BY t_order_info.pay_time DESC, t_order_info.add_time DESC ";
        Object [] args = new Object[]{userid};

        String limit = " limit " + (pageindex-1)*pagesize + "," + pagesize;
        Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" SELECT COUNT(*) " + where , args);

        List<OrderVO> list = JdbcUtil.queryForList(baseJdbcDao, OrderVO.class, sql + where + orderBy + limit, args);

        Page page = new Page(list, pageindex, totalCount, pagesize);

        return page;
    }
    public Page<OrderVO> queryNotShipedByUserid(String userid, int pageindex, int pagesize){
        String sql =
                "SELECT t_order_goods.*, t_order_info.*, t_goods_picture.pic_url\n";
        String where =
                "                FROM t_order_info,\n" +
                "                     t_order_goods\n"+
                "                LEFT JOIN t_goods_picture ON t_order_goods.goods_id = t_goods_picture.goods_id AND t_goods_picture.is_main = 1\n"+
                "                WHERE t_order_goods.order_id = t_order_info.order_id\n" +
                "                AND t_order_info.order_status = 1\n"+
                "                AND t_order_info.pay_status = 1\n"+
                "                AND t_order_info.invalid =0\n"+
                "                AND t_order_info.shipping_status = 0\n"+
                "                AND t_order_info.user_id = ?\n";
        String orderBy =
                "                ORDER BY t_order_info.pay_time DESC, t_order_info.add_time DESC ";
        Object [] args = new Object[]{userid};

        String limit = " limit " + (pageindex-1)*pagesize + "," + pagesize;
        Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" SELECT COUNT(*) " + where , args);

        List<OrderVO> list = JdbcUtil.queryForList(baseJdbcDao, OrderVO.class, sql + where + orderBy + limit, args);

        Page page = new Page(list, pageindex, totalCount, pagesize);

        return page;
    }
    public Page<OrderVO> queryRefoundByUserid(String userid, int pageindex, int pagesize){
        String sql =
                "SELECT t_order_goods.*, t_order_info.*, t_goods_picture.pic_url\n";
        String where =
                "                FROM t_order_info,\n" +
                "                     t_order_goods\n"+
                "                LEFT JOIN t_goods_picture ON t_order_goods.goods_id = t_goods_picture.goods_id AND t_goods_picture.is_main = 1\n"+
                "                WHERE t_order_goods.order_id = t_order_info.order_id\n" +
                "                AND t_order_info.is_refund >= 1\n"+
                "                AND t_order_info.invalid =0\n"+
                "                AND t_order_info.user_id = ?\n";
        String orderBy =
                "                ORDER BY t_order_info.pay_time DESC, t_order_info.add_time DESC ";
        Object [] args = new Object[]{userid};

        String limit = " limit " + (pageindex-1)*pagesize + "," + pagesize;
        Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" SELECT COUNT(*) " + where , args);

        List<OrderVO> list = JdbcUtil.queryForList(baseJdbcDao, OrderVO.class, sql + where + orderBy + limit, args);

        Page page = new Page(list, pageindex, totalCount, pagesize);

        return page;
    }
}
